"use strict";
// Class definition

var RectifyFollowUpList = function () {

    var datatable;
    var eventHandler = function () {
        eventsCapture();

        $('.k_selectpicker').selectpicker();

        $('#k_datepicker_1').datepicker({
            format: 'yyyy-mm-dd'
        });

        $('#k_datepicker_2').datepicker({
            format: 'yyyy-mm-dd'
        });

        $(document).on('click', '.btn-search', doSearch);
    };
    // Private functions
    // basic demo
    var initFollowUpList = function () {

        datatable = $('#follow_up_list').KDatatable({
            // datasource definition
            data: {
                type: 'remote',
                source: {
                    read: {
                        url: 'json/follow-up-list.json',
                        method: 'GET',
                        /**  格式化数据（默认）
                         map: function(raw) {
							// sample data mapping
							var dataSet = raw;
							if (typeof raw.data !== 'undefined') {
								dataSet = raw.data;
							}
							return dataSet;
						},
                         */
                    },
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
            },

            // layout definition
            layout: {
                scroll: false,
                footer: false,
            },

            // column sorting
            sortable: false,

            pagination: true,

            translate: {
                records: {
                    processing: '数据加载中...',
                    noRecords: '未匹配到对应数据',
                },
                toolbar: {
                    pagination: {
                        items: {
                            default: {
                                first: '第一页',
                                prev: '前一页',
                                next: '下一页',
                                last: '最后一页',
                                more: '更多',
                                input: 'Page number',
                                select: '选择页面显示数',
                            },
                            info: '显示 {{start}} - {{end}} 的 {{total}}',
                        },
                    },
                },
            },

            search: {
                input: $('#generalSearch'),
            },

            // columns definition
            columns: [
                {
                    field: 'discovery_site',
                    title: '发现地点'
                }, {
                    field: 'problems_hidden_trouble',
                    title: '问题隐患'
                }, {
                    field: 'parsing_time',
                    title: '分析时间'
                }, {
                    field: 'newsfrom',
                    title: '信息来源'
                }, {
                    field: 'measures_type',
                    title: '措施类型'
                }, {
                    field: 'item_measures',
                    title: '措施项'
                }, {
                    field: 'measures_time_limit',
                    title: '措施期限'
                }, {
                    field: 'delay_time',
                    title: '延期时间'
                }, {
                    field: 'individual_progress',
                    title: '单项进度',
                    width: 100
                }, {
                    field: 'accessory',
                    title: '附件',
                    width: 100,
                    template: function () {
                        return '<a class="handle" href="javascript:;">浏览</a>';
                    }
                }, {
                    field: 'handle',
                    title: '处理',
                    width: 100,
                    template: function () {
                        return '<a class="handle" href="">处理</a>';
                    }
                }],

        });

        $('#k_form_status').on('change', function () {
            // datatable.search($(this).val().toLowerCase(), 'status');
        });

        $('#k_form_type').on('change', function () {
            // datatable.search($(this).val().toLowerCase(), 'type');
        });

    };

    var eventsCapture = function () {
        $('.k_datatable').on('k-datatable--on-init', function () {
            console.log('Datatable init');
        }).on('k-datatable--on-layout-updated', function () {
            console.log('Layout render updated');
        }).on('k-datatable--on-ajax-done', function () {
            console.log('Ajax data successfully updated');
        }).on('k-datatable--on-ajax-fail', function (e, jqXHR) {
            console.log('Ajax error');
        }).on('k-datatable--on-goto-page', function (e, args) {
            console.log('Goto to pagination: ' + args.page);
        }).on('k-datatable--on-update-perpage', function (e, args) {
            console.log('Update page size: ' + args.perpage);
        }).on('k-datatable--on-reloaded', function (e) {
            console.log('Datatable reloaded');
        }).on('k-datatable--on-check', function (e, args) {
            console.log('Checkbox active: ' + args.toString());
        }).on('k-datatable--on-uncheck', function (e, args) {
            console.log('Checkbox inactive: ' + args.toString());
        }).on('k-datatable--on-sort', function (e, args) {
            console.log('Datatable sorted by ' + args.field + ' ' + args.sort);
        });
    };

    var doSearch = function () {
        var query = datatable.getDataSourceQuery();
        query.newsfrom = $('select.newsfrom').val();
        query.discoverySite = $('select.discovery_site').val();
        query.generalSearch = $('#generalSearch').val();
        query.parsingTime = $(".parsing_time").val();
        query.measuresTimeLimit = $('.measures_time_limit').val();
        datatable.setDataSourceQuery(query);
        datatable.search();
    };

    return {
        // public functions
        init: function () {
            eventHandler();
            initFollowUpList();
        },
    };
}();

jQuery(document).ready(function () {
    RectifyFollowUpList.init();
});